<template>
    <el-dialog
        width="671px"
        :visible.sync="dialogVisible"
        custom-class="pic-preview-component">
        <slot />
        <el-icon
            name="close"
            @click.native="dialogVisible = false" />
        <div class="pic-wrapper">
            <img
                class="acitve-pic"
                :src="pics[active]">
            <div class="pics">
                <span
                    v-for="(item, index) in pics"
                    :key="index"
                    :class="{ 'active': index == active}"
                    @click="active = index">
                    <img :src="item">
                </span>
            </div>
        </div>
    </el-dialog>
</template>

<script>
export default {
    props: {
        value: {
            type: Boolean,
            required: true
        },
        pics: {
            type: Array,
            required: true,
            default() {
                return [];
            }
        }
    },
    watch: {
        value(val) {
            this.dialogVisible = val;
        },
        dialogVisible(val) {
            if (val != this.value) {
                this.$emit('input', val);
            }
        }
    },
    data() {
        return {
            dialogVisible: false,
            active: 0
        };
    },
    created() {
        window.addEventListener('keydown', this.handleKeyDown);
    },
    methods: {
        handleKeyDown(event) {
            if (this.dialogVisible) {
                if (event.keyCode == 37) {
                    this.active = this.active - 1 < 0 ? this.pics.length -1 : this.active - 1;
                } else if (event.keyCode == 39) {
                    this.active = this.active + 1 >= this.pics.length ? 0 : this.active + 1;
                }
            }
        }
    },
    beforeDestroy() {
        window.removeEventListener('keydown', this.handleKeyDown);
    }
};
</script>

<style lang="less" scoped>
.el-icon-close {
    position: absolute;
    top: 5px;
    right: 5px;
    padding: 5px;
    font-size: 12px;
    cursor: pointer;
}
.pic-wrapper {
    display: flex;
    padding: 24px;
}
.acitve-pic {
    width: 508px;
    height: 340px;
}
.pics {
    height: 340px;
    margin-left: 10px;
    padding-right: 10px;
    overflow: auto;
    span {
        display: block;
        width: 90px;
        height: 54px;
        font-size: 0;
        border: 1px solid transparent;

        &.active {
            border-color: #20a0ff;
        }

        & + span {
            margin-top: 10px;
        }
    }
    img {
        width: 88px;
        height: 52px;
    }
}
</style>


<style lang="less">
.pic-preview-component {
    width: 671px;
    .el-dialog__header {
        display: none;
    }
    .el-dialog__body {
        padding: 0;
    }
}
</style>
